//
//  ENFeedbackViewController.m
//  EverydayNews
//
//  Created by 李沛池 on 2017/7/24.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "ENFeedbackViewController.h"

@interface ENFeedbackViewController ()<
UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *labelTextNum;

@end

@implementation ENFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)initUI
{
    self.title = @"留言给我们";
}

- (IBAction)commitAction:(UIButton *)sender
{
    if (_textView.text.length > 500) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"亲!最多只能输入500个字!请您合理安排内容!" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertController animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        return;
    }
    
    [LPCMBProgressHUD showSuccessWithText:@"提交成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        PopVC;
    });
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"我们需要您的反馈"]) {
        textView.text = @"";
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _textView.text = @"我们需要您的反馈";
    }
    
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    _labelTextNum.text = [NSString stringWithFormat:@"%lu/500个字",textView.text.length];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_textView resignFirstResponder];
}

@end
